//
//  UIScrollView+NoDatas.m
//  SheMeasured
//
//  Created by ddkj on 2019/5/8.
//  Copyright © 2019 xmddkj. All rights reserved.
//

#import "UIScrollView+NoDatas.h"

@implementation NoDatasView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        
        self.icon = [[UIImageView alloc] init];
        [self addSubview:self.icon];
        
        self.label = [[UILabel alloc] init];
        self.label.textAlignment = NSTextAlignmentCenter;
        self.label.font = [UIFont systemFontOfSize:15];
        self.label.numberOfLines = 0;
        self.label.userInteractionEnabled = YES;
        self.label.textColor = [UIColor grayColor];
        [self addSubview:self.label];
    }
    return self;
}

- (void)setHaveIcon:(BOOL)haveIcon {
    _haveIcon = haveIcon;
    [self layoutSubviews];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (self.haveIcon) {
        self.icon.frame = CGRectMake(self.width/2 - 100, 0, 200, 200);
    } else {
        self.icon.frame = CGRectMake(self.width/2, 0, 0, 0);
    }
    
    self.label.frame = CGRectMake(0, self.icon.bottom, self.width, self.height - self.icon.height);
}

@end



@implementation UIScrollView (NoDatas)

- (NoDatasView *)placeHolder {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPlaceHolder:(NoDatasView *)placeHolder {
    if (placeHolder) {
        objc_setAssociatedObject(self, @selector(placeHolder), placeHolder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (void (^)(void))clickBlock {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setClickBlock:(void (^)(void))clickBlock {
    if (clickBlock) {
        objc_setAssociatedObject(self, @selector(clickBlock), clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}



- (void)showPlaceHolderWithFrame:(CGRect)frame imageName:(NSString *)imageName message:(NSString *)message clickBlock:(nullable void (^)(void))clickBlock {
    self.clickBlock = clickBlock;
    
    if (self.placeHolder == nil)
    {
        self.placeHolder = [[NoDatasView alloc] initWithFrame:frame];
        [self addSubview:self.placeHolder];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRefreshAction)];
        [self.placeHolder.label addGestureRecognizer:tap];
    }
    
    self.placeHolder.label.text = message;
    
    if (imageName && ![imageName isEqualToString:@""]) {
        self.placeHolder.haveIcon = YES;
        self.placeHolder.icon.image = [UIImage imageNamed:imageName];
    } else {
        self.placeHolder.haveIcon = NO;
    }
}

- (void)tapRefreshAction {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

- (void)showPlaceHolder:(BOOL)show message:(NSString *)message {
    if (!show) {
        [self showPlaceHolderWithFrame:CGRectMake(self.centerX-100, 50, 200, 200) imageName:@"empty_noData" message:message clickBlock:nil];
    }
}

- (void)hidePlaceHolder {
    if (self.placeHolder) {
        [self.placeHolder removeFromSuperview];
    }
}


@end
